home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group92c.txt / 000073_icon-group-sender _Mon Nov 9 15:24:23 1992.msg < prev    next >
Internet Message Format  |  1993-01-04  |  6KB

  1. Received: by cheltenham.cs.arizona.edu; Tue, 10 Nov 1992 20:22:10 MST
  2. Date: 9 Nov 92 15:24:23 GMT
  3. From: csn!stortek!LSTC2VM.stortek.com!WILLS@handies.ucar.edu  (Cheyenne Wills)
  4. Organization: StorageTek SW Engineering
  5. Subject: Re: More Than LineAtATime
  6. Message-Id: <1689A7640.WILLS@LSTC2VM.stortek.com>
  7. References: <1992Nov6.204338.1332@wariat.org>
  8. Sender: icon-group-request@cs.arizona.edu
  9. To: icon-group@cs.arizona.edu
  10. Status: R
  11. Errors-To: icon-group-errors@cs.arizona.edu
  12.  
  13. With all the talk of processing strings across line boundries and
  14. performing character scanning etc.  I have an example that might
  15. address some of those.
  16.  
  17. The following program was written to strip comments and blank
  18. lines from a C program.  It handles the case where comments (or
  19. blank lines) might occur within quoted strings.
  20.  
  21. Anyway... Enjoy
  22.  
  23. =========================================================================
  24. #
  25. # Removes all C comments and blank lines from a C file.
  26. #
  27. # cmd syntax: infile outfile
  28. #
  29. global outline
  30. global in,out,quotechar,process
  31.  
  32. procedure main(args)
  33.     # Build the input and output fileids
  34.     fidi := args[1]
  35.     fido := args[2]
  36.  
  37.     in := open(fidi,"r") | stop("Unable to open ",fidi," for input")
  38.     out := open(fido,"w") | stop("Unable to open ",fido," for output")
  39.  
  40.     outline := ""                     # Start with an empty line
  41.     process := standard               # And "standard" input processing
  42.  
  43.     while line := trim(read(in)) do { # Read a line from input
  44.         if *line == 0 then next       # Throw away empty lines
  45.         line ||:= "\n"                # Add a newline (for later)
  46.         line ? while not pos(0) do    # Scan the line using current
  47.             process()                 # process
  48.         }
  49.  
  50.     flush()                           # Flush anything we have left
  51.     close(in)                         # close files
  52.     close(out)
  53. end
  54.  
  55. #
  56. # "Standard" string filter.
  57. #
  58. # This procedure looks for quotes, and start of comments.
  59. # When it finds a quoted string, it sets the current process to inquote
  60. # When it finds a comment, it sets the current process to incomment
  61. #
  62. # Moves scanned data to output.
  63. #
  64. procedure standard()
  65.  
  66.     # Write all data upto a quote or start of a comment OR the end
  67.     # of a line.  If writing to the end of a line, return.
  68.     outwrite( tab(upto('"\'/'))) | (outwrite(tab(0)) & return)
  69.  
  70.     # Found either the start of a comment or the start of a quote
  71.     if ="/*" then              # Check for start of a comment.
  72.         process := incomment   # Yep -- process is now in a comment.
  73.     else if any('\'"') then {  # Check for start of a quoted string
  74.         quotechar := move(1)   # Yep -- Get the quote delimiter
  75.         process := inquote     # Set processing for quoted strings
  76.         outwrite(quotechar)    # and dump quote char out.
  77.         }
  78.     else outwrite(move(1))     # False alarm -- just a "/" -- write it.
  79.  
  80.     return                     # And return
  81. end
  82.  
  83. #
  84. # Process a quoted string.  The string delimiter is in quotechar.
  85. # Will set process back to standard when the quotechar is found.
  86. # Handles C escaped characters
  87. #
  88. procedure inquote()
  89.     # Write everything upto either the quote char or backslash
  90.     #   OR write to the end of the line and return.
  91.     outwrite( tab(upto( quotechar ++ '\\')) ) |
  92.         (outwrite(tab(0)) & return)
  93.  
  94.     # If we have an escaped character.. then just dump it and return
  95.     outwrite(="\\") & outwrite(move(1)) & return
  96.  
  97.     outwrite( =quotechar )     # Write the quote char (hopefully)
  98.     process := standard        # set process back to standard.
  99.     return                     # And return.
  100. end
  101.  
  102. #
  103. # Process a comment.  Just throw data away until we find the
  104. # comment end string.  When end of comment is reached, set process
  105. # back to standard.
  106. #
  107. procedure incomment()
  108.  
  109.     tab(upto('*')) |  # Skip upto either an asterisk OR
  110.         (tab(0) & return)  # skip the rest of the line and return.
  111.  
  112.     if ="*/" then process := standard  # reset back to standard on EOC
  113.     else move(1)               # otherwise throw the "*" away
  114.     return                     # And return
  115. end
  116.  
  117. #
  118. # Put data to the output file.  Will not write blank lines.
  119. #
  120. procedure outwrite(data)
  121.    local xout,rest
  122.    outline ||:= data           # Append the data to the current buffer
  123.    xout := ""
  124.    rest := ""
  125.    outline ? {                 # Scan the current buffer
  126.        xout := tab(upto('\n')) & move(1) # Split on newlines
  127.        rest := tab(0)
  128.    }
  129.    xout := trim(xout)          # Trim trailing blanks
  130.    if *xout > 0 then write(out,xout) # If not an empty string then write
  131.    outline := rest             # and set the output buffer to
  132.    return                      # the remaining data and return.
  133. end
  134.  
  135. #
  136. # Flush any remaining data from our output buffer.
  137. #
  138. procedure flush()
  139.    local xout,rest
  140.    xout := ""
  141.    rest := ""
  142.    while *outline > 0 do {     # Keep processing as long as there is
  143.        outline ? {             # data left in the buffer.
  144.            xout := tab(upto('\n')) & move(1)  # Split on newlines
  145.            rest := tab(0)
  146.        }
  147.        xout := trim(xout)      # Trim trailing blanks
  148.        if *xout > 0 then write(out,xout) # If not an empty string then
  149.        outline := rest         # Write it out.  And continue with
  150.        }                       # the rest of the data.
  151.    return                      # Return when all done..
  152. end
  153. =========================================================================
  154. +---------------------------------------+-------------------------------+
  155. | +-----+                               | Cheyenne Wills                |
  156. | |     |       "From here on up it is  | Storage Technology Corporation|
  157. | |  +--+--+      downhill all the way" | 2270 South 88th St.           |
  158. | |  |  |  |                            | Louisville, Co. 80028-4232    |
  159. | +--+--+  |                            |                               |
  160. |    |     |                            | Cheyenne_Wills@stortek.com    |
  161. |    +-----+                            | cheyenne@witsend.stortek.com  |
  162. +---------------------------------------+-------------------------------+
  163.